package cn.lyjuan.first.netty._4_netty_serialize.msg.reader;

import cn.lyjuan.first.netty._4_netty_serialize.msg.reader.impl.HelloReaderImpl;
import cn.lyjuan.first.netty._4_netty_serialize.msg.reader.impl.InvalidReaderImpl;
import cn.lyjuan.first.netty._4_netty_serialize.msg.reader.impl.MsgReaderImpl;

import java.util.HashMap;
import java.util.Map;

public class ReaderFactory
{
    private static final Map<String, IReader> readers = new HashMap<>();

    private static final IReader invalidReader = new InvalidReaderImpl();

    /**
     * 根据头部信息获取
     *
     * @param head
     * @return 返回对应头部的解析器，没有对应的头部处理器，则返回空
     */
    public IReader dispatchReader(String head)
    {
        IReader reader = readers.get(head);

        return null == reader ? invalidReader : reader;
    }

    public void addReader(IReader reader)
    {
        if (null == reader) return;

        readers.put(reader.head(), reader);
    }
}
